package com.seven.monior.AppInfo;

import java.util.ArrayList;
import java.util.List;

import com.seven.monior.R;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.net.TrafficStats;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProcessItemAdapter extends BaseAdapter{

    private Context context;
    private List<RunningAppProcessInfo> items;
    private LayoutInflater mInflater;
    
    public ProcessItemAdapter(Context ctx) {
    	mInflater = LayoutInflater.from(ctx);
        items = new ArrayList<RunningAppProcessInfo>();
    }
    
    public void AddData(ArrayList<RunningAppProcessInfo> mList){
    	items = mList;
    }
    
    @Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;  
		if (convertView == null) {
              holder=new ViewHolder(); 
              convertView = mInflater.inflate(R.layout.processes_list_item, null);
              //holder.processIcon = (ImageView)convertView.findViewById(R.id.process_item_processIcon);
              holder.processName = (TextView)convertView.findViewById(R.id.process_item_processName);
              holder.processID = (TextView)convertView.findViewById(R.id.process_item_processID);
              
              holder.processLogs = (TextView)convertView.findViewById(R.id.process_item_logs);
              
              convertView.setTag(holder);
              
		}else {       
			holder = (ViewHolder)convertView.getTag();
		}
		//holder.processIcon.setImageDrawable(items.get(position).);
		holder.processName.setText((String)items.get(position).processName);
		
		holder.processID.setText(String.valueOf(items.get(position).pid));
		
		String strLog=getRunningAppInfoLog(items.get(position));
		
		holder.processLogs.setText(strLog);

		
		
		//holder.processStatus.setText((String)items.get(position).);
//		if(!items.get(position).getIsRunning()){
//			holder.appIsRunning.setVisibility(View.GONE);
//			
//		}
//		else{
//			//holder.appIsRunning.setIndeterminate(false);
//			holder.appIsRunning.setVisibility(View.VISIBLE);
//			//Log.d("SETINTERMINATE", "SETINTERMINATE");
//		}
//			
		
		           
		return convertView;
	}
	
	private String getRunningAppInfoLog(RunningAppProcessInfo item) {
		String strLog="";
		strLog+="Importance: "+item.importance+"\n";
		strLog+="Pid: "+item.pid+"\n";
		strLog+="ProcessName: "+item.processName+"\n";
		strLog+="Uid: "+item.uid+"\n";
		strLog+="PkgList: ";
		for(String pkgString : item.pkgList){
			strLog+=pkgString + " | ";
		}
		strLog+="\n";
		Long lRXBytes=TrafficStats.getUidRxBytes(item.uid);
		Long lTXBytes=TrafficStats.getUidTxBytes(item.uid);
		strLog+="RX Bytes: "+lRXBytes+"\n";
		strLog+="TX Bytes: "+lTXBytes+"\n";
		
		return strLog;
		
	}
	

}

